package com.jzg.common.cron4j;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Supplier;

import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.cron4j.ITask;
import com.jzg.common.constant.Role;
import com.jzg.common.model.biz.Cab;
import com.jzg.common.model.biz.CabUser;
import com.jzg.common.util.UuidUtil;

public class CABUSERTask implements ITask{
	private boolean isEnd = true;
	
	protected final Log log = Log.getLog(getClass());
	ExecutorService executor = Executors.newFixedThreadPool(1);

	@Override
	public void run() {
		if (!isEnd)return;
		isEnd = false;
	    CompletableFuture<Integer> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {
	        @Override
	        public Integer get() {
	        	try {
	            	//List<Record> records=Db.use("plga").find("SELECT * FROM Manage WHERE NGroup != 4 AND NState <> '-1' ");
	        		List<Record> records=Db.use("plga").find("SELECT * FROM Manage WHERE NState <> '-1' ");
	            	//List<Cab> cabs = Cab.dao.find("SELECT * FROM "+Cab.TABLE_NAME+" WHERE ID <> 'PL-JZG-53010305-2224' ");
	            	List<Cab> cabs = Cab.dao.find("SELECT * FROM "+Cab.TABLE_NAME+" WHERE 1=1 ");
	            	if(records!=null && records.size()>0 && cabs!=null && cabs.size()>0) {
	            		for (Cab cab : cabs) {
	            			StringBuffer sb = new StringBuffer();
		            		for (Record record : records) {
		            			CabUser cabUser = CabUser.dao.findFirst("select * from "+CabUser.TABLE_NAME+" where cabId=? and userId=? ",cab.getId(),record.getStr("NId"));
		            			if(cabUser==null) {
	        						cabUser = new CabUser();
	        						cabUser.setId(UuidUtil.getUUID());
	        						cabUser.setCabId(cab.getId());
	        						cabUser.setUserId(record.getStr("NId"));
	        						cabUser.save();
		            			}
		            			if (Role.roleMap.containsKey(record.getInt("NGroup"))) {
		            				cabUser.setRoleName("超级管理员");
        							cabUser.setRoleKey("setting|all");
		        				}else {
		        					cabUser.setRoleName("法制专员|");
        							cabUser.setRoleKey("setting");
		        				}
		            			cabUser.update();
		            			sb.append("'").append(record.getStr("NId")).append("'").append(",");
							}
		            		if(!StrKit.isBlank(sb.toString())) {
								Db.delete("delete from "+CabUser.TABLE_NAME+" where cabId=? and  userId not in ("+(sb.substring(0,sb.length()-1))+") AND userId NOT IN (SELECT USERID FROM BIZ_CAB_STORE_USER GROUP BY USERID )  ",cab.getId());
							}
						}
	            	}
	            } catch (Exception e) {
	                e.printStackTrace();
	            }
	        	isEnd = true;
	            return 1;
	        }
	    }, executor);
	    future.thenAccept(e -> {
	    	isEnd = true;
	    });
	}

	@Override
	public void stop() {
		
	}

}